package pruebas;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import junit.framework.TestCase;
import negModel.Aleron;
import negModel.AlgoPesos;
import negModelExcep.*;
//OK
public class AleronTest extends TestCase {

	Aleron aleron1,aleron2;
	Element xmlElement,e;
	
	protected void setUp() throws Exception {
		super.setUp();
		aleron1 = new Aleron(1000,15,5,new String("Aleron"),new AlgoPesos(100),true,5,5,8,10);
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder loader = factory.newDocumentBuilder();
		Document doc = loader.newDocument();
		
		e = doc.createElement("parte");
		e.setAttribute("Clase","aleron");
		
		e.setAttribute("maxVU", ""+ aleron1.getMaxVU());
		e.setAttribute("vidaUtil", ""+ aleron1.getVidaUtil());
		e.setAttribute("peso", ""+ aleron1.getPeso());
		e.setAttribute("aportevmax", ""+ aleron1.aportevmax());
		e.setAttribute("detalle", aleron1.getDetalle());
		e.setAttribute("precio", ""+(aleron1.getPrecio()).getMonto());
		e.setAttribute("removible", ""+aleron1.esRemovible());
		e.setAttribute("aerodinamia", ""+aleron1.getAerodinamia());
		
		e.setAttribute("parteInferior", ""+aleron1.getParteInferior());
		e.setAttribute("parteSuperior", ""+aleron1.getParteSuperior());
		e.setAttribute("largo", ""+aleron1.getLargo());
		
	}

	public void testGastar(){
		try {
			aleron1.gastar();
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
		assertEquals(999,aleron1.getVidaUtil(),0.1);
	}
	
	//double coef=((this.largo*(this.parteInferior-this.parteSuperior))/100);
	
	public void testFuncionar() {
		try {
			assertEquals(0.1,aleron1.funcionar(),0.1);
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
				
	}

	public void testGetLargo() {
		assertEquals(5,aleron1.getLargo(),0);
				
	}

	public void testGetParteInferior() {
		assertEquals(10,aleron1.getParteInferior(),0.0);
		
	}

	public void testGetParteSuperior() {
		assertEquals(8,aleron1.getParteSuperior(),0.0);
		
	}
	
	public void testGetCostoDeArreglo(){
		try {
			aleron1.gastar();
			aleron1.gastar();
			aleron1.gastar();
			aleron1.gastar();
		} catch (eParteNoFunciona e) {
			e.printStackTrace();
		}
		
		assertEquals(4,aleron1.getCostoDeArreglo().getMonto(),0.0);
		assertEquals(996,aleron1.getVidaUtil(),0.0);
		
	}
	
	public void testToXml() throws Exception{
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
		
		e=aleron1.toXml(doc);
		
		assertEquals(aleron1.getParteSuperior(),Double.parseDouble(e.getAttribute("parteSuperior")),0.0);
		assertEquals(aleron1.getParteInferior(),Double.parseDouble(e.getAttribute("parteInferior")),0.0);
		assertEquals(aleron1.getLargo(),Double.parseDouble(e.getAttribute("largo")),0.0);
		
		assertEquals(aleron1.getAerodinamia(),Integer.parseInt(e.getAttribute("aerodinamia")));
		assertEquals((aleron1.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(aleron1.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(aleron1.getDetalle(),e.getAttribute("detalle"));
		assertEquals(aleron1.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(aleron1.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
        
	}
	
	public void testAleron(){
		
		aleron2=new Aleron(e);
		
		assertEquals(aleron2.getParteSuperior(),Double.parseDouble(e.getAttribute("parteSuperior")),0.0);
		assertEquals(aleron2.getParteInferior(),Double.parseDouble(e.getAttribute("parteInferior")),0.0);
		assertEquals(aleron2.getLargo(),Double.parseDouble(e.getAttribute("largo")),0.0);
		
		assertEquals(aleron2.getAerodinamia(),Integer.parseInt(e.getAttribute("aerodinamia")));
		assertEquals((aleron2.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(aleron2.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(aleron2.getDetalle(),e.getAttribute("detalle"));
		assertEquals(aleron2.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(aleron2.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
				
	}

}
